package com.rzico.report.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.account.model.CompanyCardVo;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.core.entity.SysJob;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.report.entity.OrderSaleCountVo;
import com.rzico.report.service.OrderSaleCountService;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 订单销售统计报表
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-11-15
 */
@Api(description = "订单销售统计报表")
@RestController
@RequestMapping("/report/orderSaleReport")
public class OrderSaleReportController extends BaseController{

    @Autowired
    private OrderSaleCountService orderSaleCountService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 按商品分组统计
     *
     * @return
     */
    @ApiOperation("按商品分组统计")
    @GetMapping("/productList")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "officeId", value = "部门Id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "shopId", value = "门店ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "memberId", value = "客户ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "productId", value = "商品ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "productCategoryId", value = "商品分类ID", dataType = "Long", paramType = "query")
    })
    public CommResult<OrderSaleCountVo> productList(String productCategoryId, Long shopId, Long memberId, String officeId,
                                                    Date startDate, Long productId, Date endDate, Pageable pageable) {
        Enterprise enterprise = enterpriseService.getCurrent();
        Map<String, Object> params = new HashMap<String, Object>();
        if (startDate!=null){
            params.put("startDate",DateUtils.truncate(startDate, Calendar.DATE));
        }
        if (endDate!=null){
            params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }

        if (StringUtils.isNotEmpty(productCategoryId)){
            params.put("productCategoryId", productCategoryId);
        }
        if (null != productId){
            params.put("productId", productId);
        }
        if (null != shopId){
            params.put("shopId", shopId);
        }
        if (null != memberId){
            params.put("memberId", memberId);
        }
        if (null != officeId){
            params.put("officeId", officeId);
        }
        params.put("enterpriseId", enterprise.getId());
        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<OrderSaleCountVo> list = orderSaleCountService.selectProductList(params);
        PageResult<OrderSaleCountVo> pageResult = new PageResult<OrderSaleCountVo>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 底部汇总合计
     *
     * @return
     */
    @ApiOperation("底部汇总合计")
    @GetMapping("/summary")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "officeId", value = "部门Id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "shopId", value = "门店ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "memberId", value = "客户ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "productId", value = "商品ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "productCategoryId", value = "商品分类ID", dataType = "Long", paramType = "query")
    })
    public CommResult<OrderSaleCountVo> productSummary(String productCategoryId,Long productId, Long shopId, Long memberId,  String officeId,
                                                       Date startDate, Date endDate, Pageable pageable) {
        Enterprise enterprise = enterpriseService.getCurrent();
        Map<String, Object> params = new HashMap<String, Object>();
        if (startDate!=null){
            params.put("startDate", DateUtils.truncate(startDate, Calendar.DATE));
        }
        if (endDate!=null){
            params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }

        if (StringUtils.isNotEmpty(productCategoryId)){
            params.put("productCategoryId", productCategoryId);
        }
        if (null != shopId){
            params.put("shopId", shopId);
        }
        if (null != memberId){
            params.put("memberId", memberId);
        }
        if (null != productId){
            params.put("productId", productId);
        }
        if (null != officeId){
            params.put("officeId", officeId);
        }
        params.put("enterpriseId", enterprise.getId());
         List<OrderSaleCountVo> list = orderSaleCountService.selectSummary(params);
        return CommResult.success(list);

    }

}
